/**
 * 数据权限页面
 */
var viewflag = true;
mini.parse();
var tree = mini.get("tree1");
var grid = mini.get("grid1");
var datagrid1 = mini.get("datagrid1");
grid.load(ajaxUrl + "/role/list.do");

var roleId;
var nodeselect = '';
var nodeId = '';
var menuId = '';
var datalist = [];// 总数据
var treeClickFlag = 0;
init();

function init() {

	mini.get("sp").set({
		height : document.body.clientHeight - 40
	});
	// $("#myiframe").css({
	// height : document.body.clientHeight - 40
	// });

	// 加载角色
	$.ajax({
		type : 'POST',
		url : ajaxUrl + "/role/list.do",
		dataType : 'json',
		contentType : "application/json",
		success : function(data) {
			// console.log(data)
			datagrid1.setData(data.data);

		},
		error : function(jqXHR, textStatus, errorThrown) {
			alert('获取失败！');
		}
	})
}

function onRoleChanged(e) {
	// console.log(e.selected.roleId);
	roleId = e.selected.roleId;

	// 加载左侧树
	tree.loading();
	tree.load(ajaxUrl + "/roleMathDataList/menusList.do?roleId="+roleId);

	// 保存上一个菜单的表格数据
	tree.on("beforenodeselect", function(e) {
		treeClickFlag++;
		if(treeClickFlag > 1) {
			savePre();
		}
	});

	// 显示表格，设置选中
	tree.on("nodeselect", function(e) {
		console.log(e.selected);
		nodeselect = e.selected;
		nodeId = e.selected.id;
		menuId = nodeId.substring(nodeId.indexOf("_") + 1);

		// grid.loading();
		// 加载右侧表格
		$.ajax({
			type : 'POST',
			url: ajaxUrl+"/roleMathDataList/findByModularAndData.do?formName=" + e.selected.name,
			dataType : 'json',
//			contentType: "application/json",
			success: function (data) {

				var selects;
				var rows = [];
				var checkFlag = false;
				grid.loading();
				grid.set({
					columns : $.parseJSON(data.colum)
				});
				grid.setData(data.data[0]);

				for (var k = 0; k < datalist.length; k++) {
					if (menuId == datalist[k].menuId) {// 修改过
						checkFlag = true;
						selects = datalist[k].roleDataString.split(',');
					}
				}
				if (!checkFlag) {// 未修改过
//					selects = data.dataSource.split(',');
					selects = [];
				}

				for (var i = 0; i < data.data[0].length; i++) {
					for (var j = 0; j < selects.length; j++) {
						if ("'"+data.data[0][i].id +"'" == selects[j]) {
							rows.push(data.data[0][i]);
						}
					}
				}
				grid.selects(rows);
			},
			error: function (jqXHR, textStatus, errorThrown) {
			alert('获取失败！');
			}
		})
	});
	setInitMask();
}

// 编辑
function update() {
	mini.get("but_update").disable();
	mini.get("but_save").enable();
	mini.get("but_abandon").enable();
	viewflag = false;
	tree.unmask();
	grid.unmask();
	datagrid1
			.mask({
				html : "<div></div>",
				cls : "mini-panel",
				style : "width: 100%; height: 100%;filter:alpha(Opacity=80);-moz-opacity:0.5;opacity: 0.5;"
			});

}

// 放弃
function abandon() {
	mini.get("but_update").enable();
	mini.get("but_save").disable();
	mini.get("but_abandon").disable();
	setInitMask();
}

// 保存
function save() {

	savePre();
	console.log(datalist);

	$.ajax({
		url : ajaxUrl + "/roleMathDataList/insertRoleMathDataList.do",
		type : "POST",
		async : false,
		contentType : "application/json",
		data : JSON.stringify(datalist),
		success : function(data) {
			// abandon();
			
			mini.alert("保存成功！")
		},
		error : function(data) {
			mini.alert("保存失败！")
		}
	});
}

function resize() {
	if (viewflag) {
		tree
				.mask({
					html : "<div></div>",
					cls : "mini-panel",
					style : "width: 100%; height: 100%;filter:alpha(Opacity=80);-moz-opacity:0.5;opacity: 0.5;"
				});
	}
}

//
function setInitMask() {
	tree
			.mask({
				html : "<div></div>",
				cls : "mini-panel",
				style : "width: 100%; height: 100%;filter:alpha(Opacity=80);-moz-opacity:0.5;opacity: 0.5;"
			});
	grid
			.mask({
				html : "<div></div>",
				cls : "mini-panel",
				style : "width: 100%; height: 100%;filter:alpha(Opacity=80);-moz-opacity:0.5;opacity: 0.5;"
			});
	datagrid1.unmask();
}

function savePre() {
	console.log(datalist, 'datalist');
	var isSelectd = false;// 是否被选过
	var selects = '';
	var nodes = grid.getSelecteds();
	// console.log(nodes);
	for (var i = 0; i < nodes.length; i++) {
		selects += "'"+nodes[i].id+"'" + ','
	}
	selects = selects.substring(0,selects.length-1);
	
	// 存数据
	for (var p = 0; p < datalist.length; p++) {
		if (datalist[p].menuId == menuId) {// 数据已存在，修改
			isSelectd = true;
			datalist[p].roleDataString = selects;
		}
	}
	if (!isSelectd && nodeselect != '') {// 数据不存在，push
		datalist.push({
			roleDataString : selects,
			menuId : menuId,
			roleId : roleId,
			modularId : ''
		})
	}
	console.log(datalist, 'datalist已保存');
}